package com.zhny.controller;

import com.github.pagehelper.Page;
import com.zhny.entity.PageResult;
import com.zhny.entity.QueryPageBean;
import com.zhny.entity.Result;
import com.zhny.pojo.Device;
import com.zhny.service.DeviceService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Reference
    private DeviceService deviceService;

    /**
     * 添加设备
     */
    @PostMapping("/add")
    public Result add(@RequestBody Device device) {
        deviceService.add(device);
        return new Result(true, "设备添加成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/findPage/{page}/{pagesize}")
    public Result findPage(@PathVariable(value = "page") Integer page, @PathVariable(value = "pagesize") Integer pagesize, @RequestParam(value = "queryString", required = false) String queryString) {
        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setCurrentPage(page);
        queryPageBean.setPageSize(pagesize);
        queryPageBean.setQueryString(queryString);
        Page<Device> pageList = deviceService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, "分页查询成功", pageResult);
    }

    /**
     * 修改
     *
     * @param device
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Device device) {
        deviceService.update(device);
        return new Result(true, "设备修改成功");
    }

    /**
     * 删除
     *
     * @param deviceId
     * @return
     */
    @DeleteMapping("/delete/{deviceId}")
    public Result delete(@PathVariable(value = "deviceId") Integer deviceId) {
        deviceService.delete(deviceId);
        return new Result(true, "设备删除成功");
    }

}
